//
//  CommunityController.m
//  吃货·上海
//
//  Created by 濮亚军 on 15/11/24.
//  Copyright © 2015年 濮亚军. All rights reserved.
//

#import "CommunityController.h"
#import "CommunityCell.h"
#import "UserShareModel.h"
#import <BmobSDK/Bmob.h>
#import "UserShareDesController.h"// 用户详情界面
#import "MJRefresh/MJRefresh.h"
#import "UIImageView+WebCache.h"
@interface CommunityController ()<UITableViewDataSource,UITableViewDelegate>

//设置tableView
@property(nonatomic, strong)UITableView *communityTableView;

//用户分享信息
@property(nonatomic, strong)UserShareModel *userShareModel;

//手势界面
@property(nonatomic, strong)UIView *tapView;
@property(nonatomic, strong)UIImageView *tapImageView;

//model数组
@property(nonatomic, strong)NSArray *userShareModelArray;
@end

@implementation CommunityController
{
    NSInteger limitNumber;  // 分页查询记录
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.frame = CGRectMake(0, 0, KScreenWidth, KScreenHeight - 64);
    
    limitNumber = 20;
    
    //设置tableView
    self.communityTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64) style:UITableViewStylePlain];
    self.communityTableView.showsVerticalScrollIndicator = NO;
    self.communityTableView.delegate = self;
    self.communityTableView.dataSource = self;
    self.communityTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_communityTableView];
    
    //注册cell
    [self.communityTableView registerClass:[CommunityCell class] forCellReuseIdentifier:@"cellIdentifier"];
    
    //手势跳转
    self.tapView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight - 64)];
    self.tapView.backgroundColor = [UIColor blackColor];
    self.tapView.alpha = 0;
    self.tapView.hidden = YES;
    [self.view addSubview:_tapView];
    self.tapImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 200, KScreenWidth, (KScreenWidth / 375) * 206)];
    self.tapImageView.userInteractionEnabled = YES;
    self.tapImageView.hidden = YES;
    self.tapImageView.alpha = 0;
    [self.tapView addSubview:_tapImageView];
    
    UITapGestureRecognizer *bigTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(bigTapAction:)];
    [self.tapImageView addGestureRecognizer:bigTap];
    
    //从数据库取出数据
    BmobQuery   *bquery = [BmobQuery queryWithClassName:@"UserPush"];
    //查找GameScore表的数据
    bquery.limit = limitNumber;
    [bquery orderByDescending:@"createdAt"];
    [bquery findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
        self.userShareModelArray = array;
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.communityTableView reloadData];
        });
    }];
    
    //添加下拉刷新
    [self.communityTableView addLegendHeaderWithRefreshingTarget:self refreshingAction:@selector(headerAction)];
    
    //上拉加载更多
    [self.communityTableView addLegendFooterWithRefreshingTarget:self refreshingAction:@selector(footerAction)];
    
}
#pragma mark-------下拉刷新-------
-(void)headerAction
{
    NSLog(@"下拉刷新");
    BmobQuery   *bquery = [BmobQuery queryWithClassName:@"UserPush"];
    //查找GameScore表的数据
    [bquery orderByDescending:@"createdAt"];
    bquery.limit = limitNumber;
    [bquery findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
        self.userShareModelArray = array;
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.communityTableView reloadData];
        });
    }];
    //让刷新状态停止
    [self.communityTableView.header endRefreshing];
}
#pragma mark-------上拉加载更多-------
-(void)footerAction
{
    NSLog(@"下拉加载更多");
    BmobQuery   *bquery = [BmobQuery queryWithClassName:@"UserPush"];
    //查找GameScore表的数据
    limitNumber = limitNumber + 5;
    bquery.limit = limitNumber;
    [bquery orderByDescending:@"createdAt"];
    [bquery findObjectsInBackgroundWithBlock:^(NSArray *array, NSError *error) {
        self.userShareModelArray = array;
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.communityTableView reloadData];
        });
    }];
    [self.communityTableView.footer endRefreshing];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.userShareModelArray.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CommunityCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cellIdentifier" forIndexPath:indexPath];
    /*
    if (cell == nil)
    {
        cell = [[CommunityCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cellIdentifier"];
    }
     */
    //cell.backgroundColor = [UIColor cyanColor];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    
    self.userShareModel = [[UserShareModel alloc]initWithbmobObject:self.userShareModelArray[indexPath.row]];
    
    [cell setUserShareModel:self.userShareModel];
    
    //UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(tapAction:)];
    //[cell.shareImageView addGestureRecognizer:tap];
    
    return cell;
}
//手势方法
-(void)tapAction:(UIGestureRecognizer *)tap
{
    [self.tapImageView sd_setImageWithURL:[NSURL URLWithString:self.userShareModel.photo] placeholderImage:[UIImage imageNamed:@"text.jpg"]];
    [UIView animateWithDuration:0.5 animations:^{
        self.communityTableView.hidden = YES;
        self.tapView.hidden = NO;
        self.tapImageView.hidden = NO;
        self.tapView.alpha = 1;
        self.tapImageView.alpha = 1;
    } completion:^(BOOL finished) {
        
    }];
}
//退出手势
-(void)bigTapAction:(UIGestureRecognizer *)tap
{
    [UIView animateWithDuration:0.5 animations:^{
        self.communityTableView.hidden = NO;
        self.tapView.alpha = 0;
        self.tapImageView.alpha = 0;
    } completion:^(BOOL finished) {
        self.tapView.hidden = YES;
        self.tapImageView.hidden = YES;
    }];
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [CommunityCell heightForCell:_userShareModel];
}


// 点击cell方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    UserShareDesController *userDesVc = [[UserShareDesController alloc]init];
    userDesVc.shareModel = [[UserShareModel alloc]initWithbmobObject:self.userShareModelArray[indexPath.row]];
    [self.navigationController pushViewController:userDesVc animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
